/**
 * 
 */
package com.worksmap.core.dao.impl;

import org.springframework.stereotype.Service;

import com.worksmap.common.GeneralDao;
import com.worksmap.core.dao.SellerConfigDao;
import com.worksmap.core.entity.SellerConfig;
import com.worksmap.core.util.ObjectUtil;
import com.worksmap.core.util.StringUtil;

/**
 * @author jack
 *
 */
@Service
public class SellerConfigDaoImpl extends GeneralDao implements SellerConfigDao {

	@Override
	public SellerConfig getSellerConfig(String taobao_user_id) {
		if(StringUtil.isEmpty(taobao_user_id)){
			return null;
		}
		SellerConfig config = null;
		try {
			config = this.getEntity(SellerConfig.class, taobao_user_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return config;
	}

	@Override
	public boolean saveSellerConfig(SellerConfig config) {
		try {
			return this.saveEntity("INSERT INTO seller_config ( " + ObjectUtil.getClassFieldStr(SellerConfig.class).replace(",id,", "") + ")" +
					"values(:taobao_user_id,:taobao_user_nick,:auto_rate_switch,:black_list_switch,:trade_rate_condition," +
					":rate_condition_day,:grab_switch)", 
					config);
		} catch (Exception e) {
			logger.error("保存用户自动评价设置SellerConfig错误！" + e);
		}
		return false;
	}

	@Override
	public boolean updateSellerConfig(SellerConfig config) {
		if(config == null){
			return false;
		}
		String[] fields = new String[]{"auto_rate_switch", "black_list_switch", "trade_rate_condition", 
		"rate_condition_day", "grab_switch"};
		return updateSellerConfigByFields(config, fields);
	}

	@Override
	public boolean updateSellerConfigByFields(SellerConfig config,
			String... fields) {
		if(config == null){
			return false;
		}
		try {
			return this.updateEntity(config, fields);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("更新用户自动评价设置SellerConfig错误！" + e);
		}
		return false;
	}

}
